package com.example.administrator.myapplication.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.TimeUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.myapplication.*;
import com.example.administrator.myapplication.MainActivity;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class RegisterActivity extends AppCompatActivity {
    EditText username;
    EditText password;
    EditText password01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        username= (EditText) findViewById(R.id.username1);
        password= (EditText) findViewById(R.id.password1);
        password01= (EditText) findViewById(R.id.password01);

    }
    public void back(View view){
        finish();
    }
    public  void register(View view){
        if (TextUtils.isEmpty(username.getText().toString())){
            Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password.getText().toString())){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (!password01.getText().toString().equals(password.getText().toString())){
            Toast.makeText(this,"两次输入的密码不一致",Toast.LENGTH_SHORT).show();
            return;
        }


        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.User.Register");

        params.addQueryStringParameter("username", username.getText().toString());
        params.addQueryStringParameter("password",password.getText().toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i("loginActivity", "onSuccess result"+result);
                Gson gson=new Gson();
                LoginModel loginModel =gson.fromJson(result,LoginModel.class);
                if (loginModel.code==200){



                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("loginActivity", "onError  result"+ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });
    }

}
